package com.hou.apizuul;

import com.google.common.util.concurrent.RateLimiter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;

/**
 * 限流过滤器
 */
@Component
public class LimitFilter extends ZuulFilter {
    //设置限流数量,可动态yml配置,这里方便起见就直接写死了
    private static volatile RateLimiter rateLimiter= RateLimiter.create(100.0);
    @Override
    public String filterType() { return "pre";}
    // 设置为最高优先级
    @Override
    public int filterOrder() { return 0; }

    @Override
    public boolean shouldFilter() {return true;}

    @Override
    public Object run() throws ZuulException {
        rateLimiter.acquire();//总体限流
        return null;
    }
}
